package com.meixiang.view.address;

import android.app.Activity;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.LinearLayout;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lzy.okhttputils.model.HttpParams;
import com.meixiang.R;
import com.meixiang.entity.account.AddressEntity;
import com.meixiang.global.Config;
import com.meixiang.http.HttpCallBack;
import com.meixiang.http.HttpUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

import okhttp3.Call;
import okhttp3.Response;

/**
 * CityPickerLayout
 * 显示地址选择的layout
 *
 * @author lZRUI
 */
public class CityPickerLayout extends LinearLayout {

    private Activity activity;
    private WheelViewAddress mProvincePicker;
    private WheelViewAddress mCityPicker;
    private WheelViewAddress mDisPicker;

    private int mCurrProvinceIndex = -1;
    private int mCurrCityIndex = -1;
    private int mCurrDisIndex = -1;

    private ArrayList<AddressEntity> proList, cityList, disList;

    public CityPickerLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        activity = (Activity) context;
    }

    public CityPickerLayout(Context context) {
        this(context, null);
        activity = (Activity) context;
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        LayoutInflater.from(getContext()).inflate(R.layout.layout_city_picker, this);
        getAddressInfo(0, "0"); // 获取省信息
        mProvincePicker = (WheelViewAddress) findViewById(R.id.province_wv);
        mCityPicker = (WheelViewAddress) findViewById(R.id.city_wv);
        mDisPicker = (WheelViewAddress) findViewById(R.id.dis_wv);

        mProvincePicker.setOnSelectListener(new WheelViewAddress.OnSelectListener() {
            @Override
            public void endSelect(int id, AddressEntity address) {
                if (address == null)
                    return;
                if (mCurrProvinceIndex != id) {
                    mCurrProvinceIndex = id;
                    // 获取城市列表
                    getAddressInfo(1, address.areaId);
                }
            }

            @Override
            public void selecting(int id, AddressEntity address) {

            }
        });

        mCityPicker.setOnSelectListener(new WheelViewAddress.OnSelectListener() {

            @Override
            public void endSelect(int id, AddressEntity address) {
                if (address == null)
                    return;
                if (mCurrCityIndex != id) {
                    mCurrCityIndex = id;
                    // 获取区域列表
                    getAddressInfo(2, address.areaId);
                }
            }

            @Override
            public void selecting(int id, AddressEntity address) {

            }
        });

        mDisPicker.setOnSelectListener(new WheelViewAddress.OnSelectListener() {
            @Override
            public void endSelect(int id, AddressEntity address) {
                if (address == null)
                    return;
                if (mCurrDisIndex != id) {
                    mCurrDisIndex = id;
                }
            }

            @Override
            public void selecting(int id, AddressEntity address) {

            }
        });

    }

    public AddressEntity getProvince() {
        if (mProvincePicker == null) {
            return null;
        }
        return mProvincePicker.getSelectedItem();
    }

    public AddressEntity getCity() {
        if (mCityPicker == null) {
            return null;
        }
        return mCityPicker.getSelectedItem();
    }

    public AddressEntity getDis() {
        if (mDisPicker == null) {
            return null;
        }
        return mDisPicker.getSelectedItem();
    }


    private void getAddressInfo(final int type , String parentId) {
        HttpParams params = new HttpParams();
        params.put("parentId", parentId);

        HttpUtils.post(Config.ADDRESS, WheelViewAddressDialog.TAG, params, new HttpCallBack(activity) {

            @Override
            public void onAfter(boolean isFromCache, JSONObject jsonObject, Call call, Response response, Exception e) {
                super.onAfter(isFromCache, jsonObject, call, response, e);
                if (type == 0) {
                    if (proList != null && !proList.isEmpty()) {
                        getAddressInfo(1, proList.get(0).areaId);
                    } else {
                        cityList.clear();
                        disList.clear();
                        mCityPicker.refreshData(cityList);
                        mDisPicker.refreshData(disList);
                    }
                }
                if (type == 1) {
                    if (cityList != null && !cityList.isEmpty()) {
                        getAddressInfo(2, cityList.get(0).areaId);
                    } else {
                        disList.clear();
                        mDisPicker.refreshData(disList);
                    }
                }
            }

            @Override
            public void onSucceed(JSONObject result, String code, String message) {
                try {
                    ArrayList<AddressEntity> address = new Gson().fromJson(result.getString("list"), new TypeToken<ArrayList<AddressEntity>>() {
                    }.getType());

                    switch (type) {
                        // 省
                        case 0:
                            proList = address;
                            mProvincePicker.setData(proList);
                            mProvincePicker.setDefault(0);
                            break;
                        // 市
                        case 1:
                            cityList = address;
                            mCityPicker.refreshData(cityList);
                            mCityPicker.setDefault(0);
                            break;
                        // 区
                        case 2:
                            disList = address;
                            mDisPicker.refreshData(disList);
                            mDisPicker.setDefault(0);
                            break;
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(String code, String message) {

            }
        });
    }
}
